package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysBookType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysBookTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 图书类型Controller
 * 
 * @author ruoyi
 * @date 2021-10-14
 */
@RestController
@RequestMapping("/system/type")
public class SysBookTypeController extends BaseController
{
    @Autowired
    private ISysBookTypeService sysBookTypeService;

    /**
     * 查询图书类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public AjaxResult list(SysBookType sysBookType)
    {

        List<SysBookType> list = sysBookTypeService.selectSysBookTypeList(sysBookType);
        return AjaxResult.success(list);
    }

    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysBookType sysBookType)
    {
        List<SysBookType> sysBookTypes = sysBookTypeService.selectSysBookTypeList(sysBookType);
        return AjaxResult.success(sysBookTypeService.buildsysBookTypeTreeSelect(sysBookTypes));
//        List<SysDept> depts = deptService.selectDeptList(dept);
//        return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
    }

    /**
     * 导出图书类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "图书类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysBookType sysBookType)
    {
        List<SysBookType> list = sysBookTypeService.selectSysBookTypeList(sysBookType);
        ExcelUtil<SysBookType> util = new ExcelUtil<SysBookType>(SysBookType.class);
        return util.exportExcel(list, "图书类型数据");
    }

    /**
     * 获取图书类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return AjaxResult.success(sysBookTypeService.selectSysBookTypeByTypeId(typeId));
    }

    /**
     * 新增图书类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "图书类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBookType sysBookType)
    {
        if (UserConstants.NOT_UNIQUE.equals(sysBookTypeService.checkbookTypeNameUnique(sysBookType)))
        {
            return AjaxResult.error("新增部门'" + sysBookType.getTypeName() + "'失败，部门名称已存在");
        }
        sysBookType.setCreateBy(getUsername());
        return toAjax(sysBookTypeService.insertSysBookType(sysBookType));
    }

    /**
     * 修改图书类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "图书类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBookType sysBookType)
    {
        sysBookType.setUpdateBy(getUsername());
        return toAjax(sysBookTypeService.updateSysBookType(sysBookType));
    }

    /**
     * 删除图书类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "图书类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long[] typeIds)
    {
        return toAjax(sysBookTypeService.deleteSysBookTypeByTypeIds(typeIds));
    }
}
